package com.bigcms.controller;

import com.bigcms.domain.Art;
import com.bigcms.mapper.ArtMapper;
import com.bigcms.service.ArtService;
import com.bigcms.utils.JsonData;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("api/v1/pri/art")
public class ArtController {

    @Autowired
    ArtService artService;
    /**
     * 发布文章
     */
    @RequestMapping("save")
    public JsonData save(@RequestBody Art art, HttpServletRequest request){
        Integer id = (Integer) request.getAttribute("uid");
        art.setUid(id);
        art.setCreateTime( (int) (System.currentTimeMillis() / 1000));
        artService.add(art);
        return JsonData.Success();
    }
    /**
     * 上传文件
     */
    @Value("${suffix}")
    private String endlist;
    @PostMapping("upload")
    @ResponseBody
    public JsonData upload(@RequestParam("file") MultipartFile file,HttpServletRequest hsr){
        if( file.isEmpty()){
            return JsonData.Error("文件不存在");
        }
        String orginName = file.getOriginalFilename();
        String suffix = orginName.substring(orginName.lastIndexOf("."));
        if(!endlist.contains(suffix)){
            return JsonData.Error("文件后缀不支持");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        String format = sdf.format(new Date());
        String realPath = System.getProperty("user.dir")+"/upload"+format;
        System.out.println("上传地址"+realPath);
        File folder = new File(realPath);
        if(!folder.exists()){
            folder.mkdirs();
        }
        String newName = UUID.randomUUID().toString()+suffix;
        try {
            file.transferTo(new File(folder,newName));
            Map<String,String> maps = new HashMap();
            maps.put("url",hsr.getScheme()+"://"+hsr.getServerName()+":"+hsr.getServerPort()+"/upload"+format+newName);
            return JsonData.Success(maps);
        } catch (IOException e) {
            e.printStackTrace();
            return JsonData.Error();
        }
    }
    /**
     * 修改文章
     */
    @RequestMapping("edit")
    public JsonData edit(@RequestBody Art art, HttpServletRequest request){
        Integer id = (Integer) request.getAttribute("uid");
        art.setUid(id);
        art.setUpdateTime( (int) (System.currentTimeMillis() / 1000));
        artService.edit(art);
        return JsonData.Success();
    }
    /**
     * 删除文章
     */
    @RequestMapping("del")
    public JsonData del(@RequestBody Art art){
        artService.del(art.getId());
        return JsonData.Success();
    }

    /**
     * 获取单篇文章
     */
    @RequestMapping("find")
    public JsonData find(@RequestBody Art art){
        Art art1 = artService.get(art.getId());
        return JsonData.Success(art1);
    }
    /**
     * 获取文章列表
     */
    @RequestMapping("list")
    public JsonData list(){
        List<Art> art1 = artService.getall();
        return JsonData.Success(art1);
    }
}
